package com.hantanger.weblog.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Logger;

import redstone.xmlrpc.XmlRpcClient;

/**
 * 请求搜索引擎收录文章
 * @author lys
 *
 */
public class BlogPing {
	
	private static Logger log = Logger.getLogger(BlogPing.class.getName());
	
	private static List<String> pingInfos = new ArrayList<String>(); 
	
	static{
		try {
			Scanner in = new Scanner(new File("WEB-INF/blogping.txt"));
			while(in.hasNext()){
				pingInfos.add(in.nextLine());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void sendPing(String nickname, String blogUrl, String postUrl, String rssUrl){
		List<String> params = new ArrayList<String>();
		params.add(nickname);
		params.add(blogUrl);
		params.add(postUrl);
		params.add(rssUrl);
		for(String pingInfo : pingInfos){
			if(!pingInfo.startsWith("#")){
				String[] info = pingInfo.split("\\s+");
				ping(info[0], info[1], params);
			}
		}
	}
	
	public static void ping(String url, String method, List<String> params){
		try {
			XmlRpcClient client = new XmlRpcClient(url, true);
			log.info("blog ping url " + url);
			client.invoke(method, params);
		} catch (Exception e) {
			log.info("blog ping url:" + url + " error!");
		}
	}

}
